package io.unicorn.modules.waterfgs.controller;

import com.alibaba.fastjson.JSONObject;
import io.unicorn.common.utils.ExcelUtils;
import io.unicorn.common.utils.PageUtils;
import io.unicorn.common.utils.R;
import io.unicorn.common.validator.ValidatorUtils;
import io.unicorn.modules.sys.entity.SysDeptEntity;
import io.unicorn.modules.sys.entity.SysUserEntity;
import io.unicorn.modules.sys.service.SysDeptService;
import io.unicorn.modules.sys.shiro.ShiroUtils;
import io.unicorn.modules.water.entity.VaccountmanageEntity;
import io.unicorn.modules.waterfgs.service.VaccountmanageFgsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * ${comments}
 *
 * @author LNCG
 * @email LNCG@USTL.EDU.CN
 * @date 2021-05-07 14:06:07
 */
@RestController
@RequestMapping("waterfgs/vaccountmanagefgs")
public class VaccountmanageFgsController {
    @Autowired
    private VaccountmanageFgsService vaccountmanageFgsService;

    @Autowired
    private SysDeptService sysDeptService;


    @RequestMapping("/chartfgs")
    //@RequiresPermissions("water:taccountmanage:list")
    public R statusfgs(@RequestParam Map<String, Object> params){

        SysUserEntity user = ShiroUtils.getUserEntity();
        SysDeptEntity dept = sysDeptService.getById(user.getDeptId());
        String deptId =  '0'+dept.getOrderNum().toString();
        params.put("dept",deptId);

        List<Map<String,Object>> page = vaccountmanageFgsService.execGetChartDataFgs(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/alertlist")
    @RequiresPermissions("waterfgs:vaccountmanagefgs:list")
    public R alertlistfgs(@RequestParam Map<String, Object> params){

        SysUserEntity user = ShiroUtils.getUserEntity();
        SysDeptEntity dept = sysDeptService.getById(user.getDeptId());
        String deptId =  '0'+dept.getOrderNum().toString();
        params.put("dept",deptId);

        PageUtils page = vaccountmanageFgsService.execGetAlertFgs(params);

        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("waterfgs:vaccountmanagefgs:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = vaccountmanageFgsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{proName}")
    @RequiresPermissions("waterfgs:vaccountmanagefgs:info")
    public R info(@PathVariable("proName") String proName){
        VaccountmanageEntity vaccountmanage = vaccountmanageFgsService.getById(proName);

        return R.ok().put("vaccountmanage", vaccountmanage);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("waterfgs:vaccountmanagefgs:save")
    public R save(@RequestBody VaccountmanageEntity vaccountmanage){
        vaccountmanageFgsService.save(vaccountmanage);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("waterfgs:vaccountmanagefgs:update")
    public R update(@RequestBody VaccountmanageEntity vaccountmanage){
        ValidatorUtils.validateEntity(vaccountmanage);
        vaccountmanageFgsService.updateById(vaccountmanage);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("waterfgs:vaccountmanagefgs:delete")
    public R delete(@RequestBody String[] proNames){
        vaccountmanageFgsService.removeByIds(Arrays.asList(proNames));

        return R.ok();
    }

    @RequestMapping("/export")
    @RequiresPermissions("waterfgs:vaccountmanagefgs:list")
    public void export(@RequestParam Map<String, Object> params,
            HttpServletRequest request,
            HttpServletResponse repsonse){
        JSONObject jsonObject = JSONObject.parseObject((String)params.get("params"));
        PageUtils page = vaccountmanageFgsService.queryPage(jsonObject);
        List<?> listEntity = page.getList();

        ExcelUtils.exportXlsx(
                repsonse,
                "export",
                "io.unicorn.modules.water.entity.VaccountmanageEntity",
                listEntity);
    }
}
